class UsersController < ApplicationController
  
  layout 'base.html',:only => [:new, :edit, :show,:create, :index]
  before_filter :authorize, :only => [:edit, :update,:show]
  before_filter :error_message_follows_field_in_next_line, :only => [:new, :edit, :edit_base_info, :edit_fetion, :edit_password, :update_base_info, :update_password, :update_fetion]
  before_filter :error_message_follows_field_by_default, :only => [:new_sm,:create_consume_in_taobao_feed]
  
  # admin
  before_filter :authorize_for_admin, :only => [:index,:bind_fetion_verify, :bind_fetion, :last_sms, :new_sm]
  
  def index
    params[:search_type] = 'all' if params[:search_type].blank?
    params[:fetion_version] = 'all' if params[:fetion_version].blank?
    params[:page] = 1 if params[:page].blank?
    params[:order] = 'created_at' if params[:order].blank?
    params[:by] = 'desc' if params[:by].blank?
    params[:one_days_ago] = 1.days.ago.to_date
    if params[:high_star].blank? and params[:low_star].blank?
      params[:high_star] = 'high'
      params[:low_star] = 'low'
    end
    params[:high_star] = '' if params[:high_star].blank?
    params[:low_star] = '' if params[:low_star].blank?
    def params.order_by
      "users.#{self[:order]} #{self[:by]}"
    end
    def params.all_users?
      self[:search_type] == 'all'
    end
    def params.sm_exception_users?
      self[:search_type] == 'sm_exception'
    end
    case
    when params[:key_word].present?
      params[:nick_name_or_email_or_mobile] = "%#{params[:key_word]}%"
      case params[:fetion_version]
      when 'all'        
        @users = User.paginate :conditions => [
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) ',
            params
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and (lite_fetion_status = :lite_fetion_status) and (users.star = :high_star or users.star = :low_star) ',
            params.merge!( :lite_fetion_status => 'success' )
          ] ).size
      when 'lite'
        @users = User.paginate :conditions => [
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and fetion_version = :fetion_version',
            params.merge!( :fetion_version => 'lite' )
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and (lite_fetion_status = :lite_fetion_status) and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge!( 
              :fetion_version => 'lite',
              :lite_fetion_status => 'success'
            )
          ] ).size
      when 'lite_no_mobile'
        @users = User.paginate :conditions => [
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and fetion_version = :fetion_version and ( mobile is null or LENGTH(mobile) = 0 )',
            params.merge!( :fetion_version => 'lite' )
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and (lite_fetion_status = :lite_fetion_status) and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge!( 
              :fetion_version => 'lite',
              :lite_fetion_status => 'success'
            )
          ] ).size
      when 'lite_mobile'
        @users = User.paginate :conditions => [
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and fetion_version = :fetion_version and LENGTH(mobile) > 0',
            params.merge!( :fetion_version => 'lite' )
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and (lite_fetion_status = :lite_fetion_status) and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge!( 
              :fetion_version => 'lite',
              :lite_fetion_status => 'success'
            )
          ] ).size
      when 'lite_mobile_wait'
        @users = User.paginate :conditions => [
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and fetion_version = :fetion_version and LENGTH(mobile) > 0 and :lite_fetion_status = :lite_fetion_status',
            params.merge!( 
              :fetion_version => 'lite' ,
              :lite_fetion_status => 'wait'
            )
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and (lite_fetion_status = :lite_fetion_status) and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge!( 
              :fetion_version => 'lite',
              :lite_fetion_status => 'success'
            )
          ] ).size
      when 'lite_mobile_success'
        @users = User.paginate :conditions => [
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and fetion_version = :fetion_version and LENGTH(mobile) > 0 and :lite_fetion_status = :lite_fetion_status',
            params.merge!( 
              :fetion_version => 'lite' ,
              :lite_fetion_status => 'success'
            )
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and (lite_fetion_status = :lite_fetion_status) and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge!( 
              :fetion_version => 'lite',
              :lite_fetion_status => 'success'
            )
          ] ).size
      when 'maple20'
        @users = User.paginate :conditions => [
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and fetion_version = :fetion_version',
            params.merge!( :fetion_version => 'maple20' )
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and (lite_fetion_status = :lite_fetion_status) and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge!( 
              :fetion_version => 'lite',
              :lite_fetion_status => 'success'
            )
          ] ).size
      when 'maple20_no_mobile'
        @users = User.paginate :conditions => [
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and fetion_version = :fetion_version and (mobile is null or LENGTH(mobile) = 0)',
            params.merge!( :fetion_version => 'maple20' )
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and (lite_fetion_status = :lite_fetion_status) and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge!( 
              :fetion_version => 'lite',
              :lite_fetion_status => 'success'
            )
          ] ).size
      when 'maple20_mobile'   
        @users = User.paginate :conditions => [
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and fetion_version = :fetion_version and LENGTH(mobile) > 0',
            params.merge!( :fetion_version => 'maple20' )
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            '(nick_name like :nick_name_or_email_or_mobile or mobile like :nick_name_or_email_or_mobile or email like :nick_name_or_email_or_mobile) and (lite_fetion_status = :lite_fetion_status) and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge!( 
              :fetion_version => 'lite',
              :lite_fetion_status => 'success'
            )
          ] ).size   
      end
      @present = (@mobiles_count * 100 / @users.total_entries) if @users.total_entries > 0
    when params.all_users?
      case params[:fetion_version]
      when 'all'
        @users = User.paginate :conditions => [
            '(star = :high_star or star = :low_star)',
            params
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            ' lite_fetion_status = :lite_fetion_status and (users.star = :high_star or users.star = :low_star) and (users.lite_fetion_status = :lite_fetion_status)',
            params.merge( :lite_fetion_status => 'success')
          ] ).size
      when 'lite'
        @users = User.paginate :conditions => [
            '(star = :high_star or star = :low_star) and fetion_version = :fetion_version',
            params.merge( :fetion_version => 'lite')
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            ' lite_fetion_status = :lite_fetion_status and (users.star = :high_star or users.star = :low_star) and (users.lite_fetion_status = :lite_fetion_status)',
            params.merge( :lite_fetion_status => 'success')
          ] ).size
      when 'lite_mobile'
        @users = User.paginate :conditions => [
            '(star = :high_star or star = :low_star) and fetion_version = :fetion_version and LENGTH(mobile) > 0',
            params.merge( :fetion_version => 'lite')
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            ' lite_fetion_status = :lite_fetion_status and (users.star = :high_star or users.star = :low_star) and (users.lite_fetion_status = :lite_fetion_status)',
            params.merge( :lite_fetion_status => 'success')
          ] ).size
      when 'lite_no_mobile'
        @users = User.paginate :conditions => [
            '(star = :high_star or star = :low_star) and fetion_version = :fetion_version and (mobile is null or LENGTH(mobile) = 0)',
            params.merge( :fetion_version => 'lite')
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            ' lite_fetion_status = :lite_fetion_status and (users.star = :high_star or users.star = :low_star) and (users.lite_fetion_status = :lite_fetion_status)',
            params.merge( :lite_fetion_status => 'success')
          ] ).size
      when 'lite_mobile_wait'
        @users = User.paginate :conditions => [
            '(star = :high_star or star = :low_star) and fetion_version = :fetion_version and lite_fetion_status = :lite_fetion_status and LENGTH(mobile) > 0',
            params.merge( :fetion_version => 'lite',
              :lite_fetion_status => 'wait'
            )
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            ' lite_fetion_status = :lite_fetion_status and (users.star = :high_star or users.star = :low_star) and (users.lite_fetion_status = :lite_fetion_status)',
            params.merge( :lite_fetion_status => 'success')
          ] ).size
      when 'lite_mobile_success'
        @users = User.paginate :conditions => [
            '(star = :high_star or star = :low_star) and fetion_version = :fetion_version and lite_fetion_status = :lite_fetion_status',
            params.merge( :fetion_version => 'lite',
              :lite_fetion_status => 'success'
            )
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            ' lite_fetion_status = :lite_fetion_status and (users.star = :high_star or users.star = :low_star) and (users.lite_fetion_status = :lite_fetion_status)',
            params.merge( :lite_fetion_status => 'success')
          ] ).size
      when 'maple20'
        @users = User.paginate :conditions => [
            '(star = :high_star or star = :low_star) and fetion_version = :fetion_version',
            params.merge( :fetion_version => 'maple20')
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            ' LENGTH(mobile) > 0 and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge( :fetion_version => 'maple20')
          ] ).size
      when 'maple20_no_mobile'
        @users = User.paginate :conditions => [
            '(star = :high_star or star = :low_star) and fetion_version = :fetion_version and mobile is null',
            params.merge( :fetion_version => 'maple20')
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            ' LENGTH(mobile) > 0 and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge( :fetion_version => 'maple20')
          ] ).size
      when 'maple20_mobile'   
        @users = User.paginate :conditions => [
            '(star = :high_star or star = :low_star) and fetion_version = :fetion_version and LENGTH(mobile) > 0',
            params.merge( :fetion_version => 'maple20')
          ],
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
        @mobiles_count = User.all(
          :conditions => [ 
            ' LENGTH(mobile) > 0 and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge( :fetion_version => 'maple20')
          ] ).size   
      end
      @present = (@mobiles_count * 100 / @users.total_entries) if @users.total_entries > 0
    when params.sm_exception_users?
      case params[:fetion_version]
      when 'all'        
        @users = User.paginate :conditions => (
          params[:sm_exception].blank? ? [
            'sms.created_at >= :one_days_ago and sms.exception is not null and (users.star = :high_star or users.star = :low_star) ',
            params
          ] : [
            'sms.created_at >= :one_days_ago and sms.exception = :sm_exception and (users.star = :high_star or users.star = :low_star)',
            params
          ]),
          :include => :sms,
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
      when 'lite'
        @users = User.paginate :conditions => (
          params[:sm_exception].blank? ? [
            'sms.created_at >= :one_days_ago and sms.exception is not null and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge( :fetion_version => 'lite')
          ] : [
            'sms.created_at >= :one_days_ago and sms.exception = :sm_exception and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge( :fetion_version => 'lite')
          ]),
          :include => :sms,
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by

      when 'lite_no_mobile'
        @users = User.paginate :conditions => (
          params[:sm_exception].blank? ? [
            'sms.created_at >= :one_days_ago and sms.exception is not null and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version and mobile is null',
            params.merge( :fetion_version => 'lite')
          ] : [
            'sms.created_at >= :one_days_ago and sms.exception = :sm_exception and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version and mobile is null',
            params.merge( :fetion_version => 'lite')
          ]),
          :include => :sms,
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
      when 'lite_mobile'
        @users = User.paginate :conditions => (
          params[:sm_exception].blank? ? [
            'sms.created_at >= :one_days_ago and sms.exception is not null and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version and LENGTH(mobile) > 0',
            params.merge( :fetion_version => 'lite')
          ] : [
            'sms.created_at >= :one_days_ago and sms.exception = :sm_exception and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version and LENGTH(mobile) > 0',
            params.merge( :fetion_version => 'lite')
          ]),
          :include => :sms,
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
      when 'lite_mobile_wait'
        @users = User.paginate :conditions => (
          params[:sm_exception].blank? ? [
            'sms.created_at >= :one_days_ago and sms.exception is not null and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version and lite_fetion_status = :lite_fetion_status and LENGTH(mobile) > 0',
            params.merge( 
              :fetion_version => 'lite',
              :lite_fetion_status => 'wait'
            )
          ] : [
            'sms.created_at >= :one_days_ago and sms.exception = :sm_exception and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version and lite_fetion_status = :lite_fetion_status',
            params.merge( 
              :fetion_version => 'lite',
              :lite_fetion_status => 'wait'
            )
          ]),
          :include => :sms,
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
      when 'lite_mobile_success'
        @users = User.paginate :conditions => (
          params[:sm_exception].blank? ? [
            'sms.created_at >= :one_days_ago and sms.exception is not null and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version and lite_fetion_status = :lite_fetion_status',
            params.merge( 
              :fetion_version => 'lite',
              :lite_fetion_status => 'success'
            )
          ] : [
            'sms.created_at >= :one_days_ago and sms.exception = :sm_exception and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version and lite_fetion_status = :lite_fetion_status',
            params.merge( 
              :fetion_version => 'lite',
              :lite_fetion_status => 'success'
            )
          ]),
          :include => :sms,
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
      when 'maple20'
        @users = User.paginate :conditions => (
          params[:sm_exception].blank? ? [
            'sms.created_at >= :one_days_ago and sms.exception is not null and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge( :fetion_version => 'maple20')
          ] : [
            'sms.created_at >= :one_days_ago and sms.exception = :sm_exception and (users.star = :high_star or users.star = :low_star) and fetion_version = :fetion_version',
            params.merge( :fetion_version => 'maple20')
          ]),
          :include => :sms,
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
      when 'maple20_no_mobile'
        @users = User.paginate :conditions => (
          params[:sm_exception].blank? ? [
            'sms.created_at >= :one_days_ago and sms.exception is not null and (users.star = :high_star or users.star = :low_star) and mobile is null and  fetion_version = :fetion_version',
            params.merge( :fetion_version => 'maple20')
          ] : [
            'sms.created_at >= :one_days_ago and sms.exception = :sm_exception and (users.star = :high_star or users.star = :low_star) and mobile is null and fetion_version = :fetion_version',
            params.merge( :fetion_version => 'maple20')
          ]),
          :include => :sms,
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by
      when 'maple20_mobile'    
        @users = User.paginate :conditions => (
          params[:sm_exception].blank? ? [
            'sms.created_at >= :one_days_ago and sms.exception is not null and (users.star = :high_star or users.star = :low_star) and LENGTH(mobile) > 0 and  fetion_version = :fetion_version',
            params.merge( :fetion_version => 'maple20')
          ] : [
            'sms.created_at >= :one_days_ago and sms.exception = :sm_exception and (users.star = :high_star or users.star = :low_star) and LENGTH(mobile) > 0 and fetion_version = :fetion_version',
            params.merge( :fetion_version => 'maple20')
          ]),
          :include => :sms,
          :page => params[:page],
          :per_page => 10,
          :order => params.order_by  
      end
      @sm_exceptions = Sm.all(
        :select => 'distinct sms.exception',
        :conditions => [
          'sms.created_at >= :one_days_ago and sms.exception is not null and (users.star = :high_star or users.star = :low_star) ',
          params
        ],
        :include => :user,
        :group => 'sms.exception',
        :order => 'sms.exception asc'
      )
      @present = (@users.total_entries * 100 / User.all.size)
    else
      raise '未实现'
    end
    respond_to do |format|
      format.html do        
        @seo_title = "麦吐司网-会员管理"
        @seo_keywords = @seo_title
        @seo_description = @seo_title
      end
            
      format.js do 
        render :layout => false
      end
    end
  end
  
  def remember_current_url_as_http_referer
  end
      
  def new
    @user = User.new(
      :mobile => session[:mobile],
      :encrypted_fetion_password => session[:fetion_password]
    )
    @http_referer = params[:http_referer] if params[:http_referer].present?
  end
  
  def create    
    @http_referer = params[:http_referer] if params[:http_referer].present?
    @http_referer = nil if @http_referer and @http_referer.match('/homes/default')
    @user = User.new(params[:user])
    @user.fetion_version = 'lite'
    #@user.nick_name = @user.email
    @user.login_at = Time.now
    if @user.save
      @user.enjoy_default_tools!
      session[:current_user_id] = @user.id
      redirect_to(@http_referer || user_url(@user))
    else
      render :new
    end
  end
  
  def edit
    @user = current_user
    @seo_title = "麦吐司网-#{@user.nick_name}-个人信息"
    @seo_keywords = "#{@user.nick_name},个人信息"
    @seo_description = @seo_title
  end
  
  def edit_base_info
    @user = current_user
  end
  
  def edit_fetion
    @user = current_user
    @user.decode_fetion_password!
    @fetion_robot = current_user.fetion || Fetion.empty_one
    @verify = LiteFetion.add_buddy_verify @fetion_robot.mobile
  end
  
  def edit_password
    @user = current_user
    @user.encrypted_password = nil
  end  
  
  def update_base_info
    @user = current_user
    if @user.update_attributes(params[:user])
      @user.try_to_create_sex_feed
      redirect_to :action => :show_base_info, :format => :js
    else
      render :edit_base_info
    end
  end
  
  def update_fetion_for_test    
    @user = current_user
    @fetion_robot = Fetion.find_by_mobile(params[:mobile])
  end
  
  def update_fetion
    @user = current_user
    @fetion_robot = Fetion.find_by_mobile(params[:mobile])    
    current_user.mobile = params[:user][:mobile]
    if current_user.invalid?
      render :edit_fetion
    else
      @lite_fetion = current_user.bind_fetion_by :robot_mobile => @fetion_robot.mobile, 
        :mobile => params[:user][:mobile],
        :verify_code => params[:verify_code]
      if @lite_fetion['result'] == 'SUCCESS' 
        logger.debug "操作成功"
      elsif @lite_fetion['result'] == 'BUDDY_EXISTS'
        redirect_to(:action => :show_fetion, :format => :js)
      else
        @verify = LiteFetion.add_buddy_verify params[:mobile]        
        @error = @lite_fetion['result']
        render :edit_fetion, :status => 500
      end
    end
  end
  
  def update_password
    @user = current_user
    if @user.update_attributes(params[:user])
      redirect_to :action => :show_password, :format => :js
    else
      render :edit_password
    end
  end  
  
  def show_base_info
  end
  
  def show_fetion
  end
  
  def show_password
  end  
  
  def update
  end
  
  def show
    @user = current_user
    @hot_posts = HotPost.all :limit => 10, :order => 'rand()'
    @left_settings = UserToolSetting.all_by_current_user_by_column 1
    @mid_settings = UserToolSetting.all_by_current_user_by_column 2
    @right_settings = UserToolSetting.all_by_current_user_by_column 3
    @seo_title = "麦吐司网-#{@user.nick_name}-吐司大拼盘"
    @seo_keywords = "#{@user.nick_name},吐司大拼盘"
    @seo_description = @seo_title
  end
  
  def update_portalets_layout
    current_user.resort_user_tool_settings(
      :settings => UserToolSetting.all_by_ids(params[:left_portalets]) ,
      :column => 1
    )
    current_user.resort_user_tool_settings(
      :settings => UserToolSetting.all_by_ids(params[:mid_portalets]) ,
      :column => 2
    )
    current_user.resort_user_tool_settings(
      :settings => UserToolSetting.all_by_ids(params[:right_portalets]) ,
      :column => 3
    )
    render :text => '', :status => 200
  end

  def send_password_sm
    send_sm_and_render do
      user = User.find_by_nick_name_or_email_or_mobile(params[:nick_name_or_password_or_mobile])      
      user.present? ? user.send_password_sm! : raise( '该用户名尚未注册，赶紧吧？')
    end
  end

  def send_password_email
    send_email_and_render do
      user = User.find_by_nick_name_or_email_or_mobile(params[:nick_name_or_password_or_mobile])      
      user.present? ? user.send_password_email! : raise( '该用户名尚未注册，赶紧吧？')
    end
  end
  
  def check_fetion_changed
  end
  
  def update_fetion_in_web_fetion_portalet
    current_user.update_fetion!(params)    
    render :text => '', :status => 200
  end
  
  def error_message_follows_field
  end
    
  # 校验错误信息显示在field后面  
  def error_message_follows_field_in_next_line
    ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|      
      if instance.error_message.kind_of?(Array)
        %(<span class="validation_error">#{html_tag}<br>&nbsp;  #{instance.error_message.join(',')}</span>)
      else
        %(<span class="validation_error">#{html_tag}<br>&nbsp;  #{instance.error_message}</span>)
      end
    end
  end
  
  def inverse_star
    @user = User.find(params[:id])
    @user.inverse_star!
    render :text => @user.star, :status => 200
  end
  
  def bind_fetion_verify
    @user = User.find(params[:id])
    @fetion_robot = @user.fetion || Fetion.empty_one
    @lite_fetion = LiteFetion.new(
      :id => @fetion_robot.mobile
    ).get(:add_buddy_verify)
  end
  
  def bind_fetion
    @user = User.find(params[:id])  
    @user.mobile = params[:dist_mobile]
    @fetion_robot = Fetion.find(params[:fetion_id])   
    if current_user.invalid?
      render :bind_fetion_verify
    else
      @lite_fetion = @user.bind_fetion_by :robot_mobile => @fetion_robot.mobile, 
        :mobile => params[:dist_mobile],
        :fetion => params[:fetion_id],
        :verify_code => params[:verify_code]
      if @lite_fetion['result'] == 'SUCCESS' 
        logger.debug "操作成功"
      elsif @lite_fetion['result'] == 'BUDDY_EXISTS'
        logger.debug "已经是好友"
      else
        @verify = LiteFetion.add_buddy_verify @fetion_robot.mobile
        @error = @lite_fetion['result']
        render :bind_fetion_verify, :status => 500
      end
    end
  end
  
  def feeds
    @user = User.find(params[:id])
    @feeds = Feed.find_all_by_user_id(@user.id, :order => 'created_at desc', :limit => 50)
  end
  
  def last_sms
    @user = User.find(params[:id])
  end
  
  def new_sm 
    @sm = Sm.new :user_id => params[:id]
    @user = @sm.user    
  end
  
  def create_sm
    @user = User.find(params[:sm][:user_id])
    @sm = Sm.new params[:sm]    
    @sm.save ? (
      render :last_sms
    ) : (
      params[:id] = params[:user_id]      
      render :new_sm 
    ) 
  end
  
  def new_consume_in_taobao_feed
    @feed = Feed::MoneyConsumedInTaobao.new :user_id => params[:id]
    @user = @feed.user
  end
  
  def create_consume_in_taobao_feed
    @feed = Feed::MoneyConsumedInTaobao.new params[:feed_money_consumed_in_taobao]
    @user = @feed.user
    @feed.save ? (
      @feeds = Feed.find_all_by_user_id(@user.id, :order => 'created_at desc', :limit => 50)
      render :feeds
    ) : (            
      render :new_consume_in_taobao_feed
    )
  end
  
  def destroy_consume_in_taobao_feed
    @feed = Feed::MoneyConsumedInTaobao.find(params[:id])
    @feed.call_back
    @feeds = Feed.find_all_by_user_id(@user.id, :order => 'created_at desc', :limit => 50)   
    render :feeds
  end
end
